package com.wu;

import java.io.File;

/**
 * 有红绿蓝三种颜色的n个石头，随机装在n个容器中，每个石头的颜色只能看一次，交换操作不限，最终保证按红绿蓝的顺序排好序
 * 解题：只能看一次就是指只能比较一次，红色无脑放在最前面，蓝色无脑放在最后面，最终就有序了
 * 砾石排序
 */
public class SmallStoneSort {

    private static final String RED = "R";
    private static final String GREEN = "G";
    private static final String BLUE = "B";

    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
        System.out.println(file.getPath());
        String[] stoneArray = {"B1", "B2", "R3", "G4", "R5", "G6", "B7", "B8", "R9", "G10", "R11"};
        String[] strings = sortSmallStone(stoneArray);
        System.out.println("++++++++++++排序结束++++++++++++");
        printArray(strings);
        System.out.println("总共" + strings.length + "个石头");

        String[] stoneArray2 = {"R", "G", "R", "G", "B", "B", "R", "G", "R"};
        String[] strings2 = sortSmallStone(stoneArray2);
        System.out.println("++++++++++++排序结束++++++++++++");
        printArray(strings2);
        System.out.println("总共" + strings2.length + "个石头");

        System.out.println("========================================================");
        String[] stoneArray3 = {"B", "R", "G", "R", "G", "B", "R", "R", "G", "B", "B", "R", "G", "R"};
        String[] strings1 = sortSmallStone2(stoneArray3);
        printArray(strings1);
    }

    /**
     * 对随机的红绿蓝石头排序
     *
     * @param arrayStone
     * @return
     */
    public static String[] sortSmallStone(String[] arrayStone) {
        int length = arrayStone.length;
        System.out.println("总共" + length + "个石头");
        printArray(arrayStone);
        int count = 0;
        for (int i = 0; i < length; i++) {
            System.out.println("=======第" + (++count) + "个石头是" + arrayStone[i] + "色=======");
            if (arrayStone[i].contains(RED) && i != 0) {
                // 红色的放在最前面，其他的后移
                System.out.println("R:");
                String temp = arrayStone[0];
                arrayStone[0] = arrayStone[i];
                arrayStone[i] = temp;
                for (int j = 1; j <= i; j++) {
                    arrayStone[i] = arrayStone[j];
                    arrayStone[j] = temp;
                    temp = arrayStone[i];
                }
                printArray(arrayStone);
            } else if (arrayStone[i].contains(BLUE) && i != length - 1) {
                // 蓝色的放在最后面，其他的前移
                System.out.println("B:");
                String temp = arrayStone[length - 1];
                arrayStone[length - 1] = arrayStone[i];
                arrayStone[i] = temp;
                for (int j = length - 2; j >= i; j--) {
                    arrayStone[i] = arrayStone[j];
                    arrayStone[j] = temp;
                    temp = arrayStone[i];
                }
                printArray(arrayStone);
                // 向前移了一位所以要--，配合循环上的++，即是继续当前位置进行判断
                i--;
            }
            // 所有的颜色查看结束
            if (count == length) {
                break;
            }
        }
        return arrayStone;
    }

    /**
     * 对随机的红绿蓝石头排序
     *
     * @param arrayStone
     * @return
     */
    public static String[] sortSmallStone2(String[] arrayStone) {
        int length = arrayStone.length;
        System.out.println("总共" + length + "个石头");
        printArray(arrayStone);
        int redIndex = 0;
        int blueIndex = length - 1;
        int i = 0;// 遍历元素的index
        // 遍历到蓝色的位置即结束或者变例的蓝色位置小于等于红色的位置
        while (redIndex < blueIndex && i < blueIndex) {
            if (arrayStone[i].contains(BLUE)) {
                // 由于蓝色是与后面没遍历过的进行交换，所以交换后继续对当前位置的颜色进行判断，即不进行i++
                String temp = arrayStone[blueIndex];
                arrayStone[blueIndex] = arrayStone[i];
                arrayStone[i] = temp;
                blueIndex--;
            } else {
                if (arrayStone[i].contains(RED)) {
                    // i与redIndex相等时不需要交换
                    if (i != redIndex) {
                        String temp = arrayStone[redIndex ];
                        arrayStone[redIndex] = arrayStone[i];
                        arrayStone[i] = temp;
                    }
                    redIndex++;
                }
                i++;
            }
            printArray(arrayStone);
            System.out.println(redIndex + "<==>" + blueIndex);
        }
        return arrayStone;
    }

    public static void printArray(String[] arrayStr) {
        for (int i = 0; i < arrayStr.length; i++) {
            System.out.print(arrayStr[i] + "  ");
        }
        System.out.println();
    }
}